package com.liangxy.news.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.liangxy.news.model.News;

import java.util.List;

/**
 * DAO for news
 */
@Dao
public interface NewsDao {
    
    /**
     * Insert a news item
     * @param news News item to insert
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(News news);
    
    /**
     * Insert multiple news items
     * @param newsList List of news items to insert
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertAll(List<News> newsList);
    
    /**
     * Update news in the database
     * @param news News to update
     */
    @Update
    void update(News news);
    
    /**
     * Delete news from the database
     * @param news News to delete
     */
    @Delete
    void delete(News news);
    
    /**
     * Get news by ID
     * @param id News ID
     * @return News item
     */
    @Query("SELECT * FROM news WHERE uniquekey = :id")
    News getNewsById(String id);
    
    /**
     * Get all news
     * @return LiveData list of all news
     */
    @Query("SELECT * FROM news ORDER BY date DESC")
    LiveData<List<News>> getAllNews();
    
    /**
     * Get news by category
     * @param category News category
     * @return LiveData list of news for the category
     */
    @Query("SELECT * FROM news WHERE category = :category ORDER BY date DESC")
    LiveData<List<News>> getNewsByCategory(String category);
    
    /**
     * Get followed news
     * @return List of followed news
     */
    @Query("SELECT * FROM news WHERE isFollowed = 1 ORDER BY date DESC")
    LiveData<List<News>> getFollowedNews();
    
    /**
     * Get news history ordered by last read time
     * @return List of news history
     */
    @Query("SELECT * FROM news WHERE lastReadTime > 0 ORDER BY lastReadTime DESC")
    LiveData<List<News>> getNewsHistory();
    
    /**
     * Set news as followed
     * @param uniquekey News uniquekey
     * @param isFollowed Whether the news is followed
     */
    @Query("UPDATE news SET isFollowed = :isFollowed WHERE uniquekey = :uniquekey")
    void setNewsFollowed(String uniquekey, boolean isFollowed);
    
    /**
     * Update news last read time
     * @param uniquekey News uniquekey
     * @param lastReadTime Last read time
     */
    @Query("UPDATE news SET lastReadTime = :lastReadTime WHERE uniquekey = :uniquekey")
    void updateNewsLastReadTime(String uniquekey, long lastReadTime);
    
    /**
     * Delete all news
     */
    @Query("DELETE FROM news")
    void deleteAll();
} 